package com.cgsd.heyu.ui.task;

import android.content.Context;
import android.content.Intent;
import android.util.TypedValue;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.android.library.wedgets.TitleBarView;
import com.cgsd.heyu.R;
import com.cgsd.heyu.presenter.BasePresenter;
import com.cgsd.heyu.ui.BaseActivity;
import com.cgsd.heyu.ui.BaseFragment;
import com.cgsd.heyu.ui.home.fragment.WrapperSendFragment;
import com.cgsd.heyu.ui.task.adapter.TabAdapter;
import com.cgsd.heyu.ui.task.vo.EditStepTwoVo;
import com.cgsd.heyu.ui.task.vo.EditThreeVo;
import com.cgsd.heyu.widgets.ScrollableViewPager;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: sky on 2021/3/3 11:17
 * Email: xcode126@126.com
 * Desc: 发布任务
 */
public class PublishTaskActivity extends BaseActivity<BasePresenter> {

    private MagicIndicator mIndicator;
    private TitleBarView mTitleBarView;
    private ScrollableViewPager mViewPager;

    private TabAdapter adapter;
    private String[] titles;
    private int tid = -1;
    private EditStepTwoVo editStepTwoVo;
    private EditThreeVo editThreeVo;

    public static Intent getIntent(Context context) {
        return getIntent(context, -1);
    }

    public static Intent getIntent(Context context, int tid) {
        return new Intent(context, PublishTaskActivity.class).putExtra("tid", tid);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.common_indicator_unscroll;
    }

    @Override
    protected void initView() {
        tid = getIntent().getIntExtra("tid", -1);
        mTitleBarView = findViewById(R.id.title_bar);
        mViewPager = findViewById(R.id.mViewPager);
        mIndicator = findViewById(R.id.mIndicator);
        mTitleBarView.setTitle(getString(R.string.a_my_task));
        findViewById(R.id.v_line).setVisibility(View.VISIBLE);

        titles = getResources().getStringArray(R.array.a_new_task_title);
        adapter = new TabAdapter(getSupportFragmentManager(), getFragments());
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(titles.length);

        initTitleIndicator();
    }

    @Override
    protected void initData() {

    }

    /**
     * 初始化菜单栏
     */
    private void initTitleIndicator() {
        CommonNavigator titleNavigator = new CommonNavigator(this);
        titleNavigator.setAdjustMode(true);//true 自适应居中、false 反之
        titleNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(titles[index]);
                simplePagerTitleView.setPadding(0, 0, 0, 0);
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                simplePagerTitleView.setNormalColor(ContextCompat.getColor(context, R.color.textMain));
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(context, R.color.app_color));
                simplePagerTitleView.setText(titles[index]);

//                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        mViewPager.setCurrentItem(index);
//                    }
//                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setColors(ContextCompat.getColor(context, R.color.app_color));
                return linePagerIndicator;
            }
        });
        mIndicator.setNavigator(titleNavigator);
        ViewPagerHelper.bind(mIndicator, mViewPager);
    }

    /**
     * get fragments
     *
     * @return
     */
    private List<BaseFragment> getFragments() {
        List<BaseFragment> list = new ArrayList<>();
        list.add(StepOneFragment.newInstance());
        list.add(StepTwoFragment.newInstance());
        list.add(StepThreeFragment.newInstance());
        return list;
    }

    /**
     * set current page
     *
     * @param index
     */
    public void setCurrentPage(int index) {
        index = index < 0 ? 0 : index;
        index = index < 3 ? index : 2;
        mViewPager.setCurrentItem(index, true);
    }

    /**
     * set current page
     *
     * @param index
     * @param tid
     */
    public void setCurrentPage(int index, int tid) {
        this.tid = tid;
        setCurrentPage(index);
    }

    public int getTid() {
        return tid;
    }

//    /**
//     * 获取第二步保存的草稿数据
//     * @return
//     */
//    public EditStepTwoVo getEditStepTwoVo() {
//        return editStepTwoVo;
//    }

    /**
     * 设置第二步保存的草稿数据
     * @param editStepTwoVo
     */
    public void setEditStepTwoVo(EditStepTwoVo editStepTwoVo) {
        this.editStepTwoVo = editStepTwoVo;
        transitChild(1,editStepTwoVo);
    }

    /**
     * 设置第三步保存的草稿数据
     * @param editThreeVo
     */
    public void setEditThreeVo(EditThreeVo editThreeVo) {
        this.editThreeVo = editThreeVo;
        transitChild(2,editThreeVo);
    }

//    /**
//     * 获取第三步保存的草稿数据
//     */
//    public EditThreeVo getEditThreeVo() {
//        return editThreeVo;
//    }

    /**
     * 切换子界面
     *
     * @param index
     * @param t
     */
    private void transitChild(int index, Object t) {
        if (adapter != null && index < adapter.getCount() && index >= 0) {
            Fragment item = adapter.getItem(index);
            if (item instanceof WrapperSendFragment) {
                ((WrapperSendFragment) item).onRefresh(t);
            }
        }
    }
}
